home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / pbmtocmu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  2.6 KB  |  119 lines

  1. /* pbmtocmuwm.c - read a portable bitmap and produce a CMU window manager bitmap
  2. **
  3. ** Copyright (C) 1989 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pbm.h"
  14. #include "cmuwm.h"
  15.  
  16. static void putinit ARGS(( int rows, int cols ));
  17. static void putbit ARGS(( bit b ));
  18. static void putrest ARGS(( void ));
  19. static void putitem ARGS(( void ));
  20.  
  21. void
  22. main( argc, argv )
  23.     int argc;
  24.     char* argv[];
  25.     {
  26.     FILE* ifp;
  27.     bit* bitrow;
  28.     register bit* bP;
  29.     int rows, cols, format, padright, row, col;
  30.  
  31.     pbm_init( &argc, argv );
  32.  
  33.     if ( argc > 2 )
  34.     pm_usage( "[pbmfile]" );
  35.  
  36.     if ( argc == 2 )
  37.     ifp = pm_openr( argv[1] );
  38.     else
  39.     ifp = stdin;
  40.  
  41.     pbm_readpbminit( ifp, &cols, &rows, &format );
  42.     bitrow = pbm_allocrow( cols );
  43.     
  44.     /* Round cols up to the nearest multiple of 8. */
  45.     padright = ( ( cols + 7 ) / 8 ) * 8 - cols;
  46.  
  47.     putinit( rows, cols );
  48.     for ( row = 0; row < rows; row++ )
  49.     {
  50.     pbm_readpbmrow( ifp, bitrow, cols, format );
  51.         for ( col = 0, bP = bitrow; col < cols; col++, bP++ )
  52.         putbit( *bP );
  53.     for ( col = 0; col < padright; col++ )
  54.         putbit( 0 );
  55.         }
  56.  
  57.     pm_close( ifp );
  58.  
  59.     putrest( );
  60.  
  61.     pm_close (stdout);
  62.  
  63.     exit( 0 );
  64.     }
  65.  
  66. static unsigned char item;
  67. static int bitsperitem, bitshift;
  68.  
  69. static void
  70. putinit( rows, cols )
  71.     int rows, cols;
  72.     {
  73.     if ( pm_writebiglong( stdout, CMUWM_MAGIC ) == -1 )
  74.     pm_error( "write error" );
  75.     if ( pm_writebiglong( stdout, cols ) == -1 )
  76.     pm_error( "write error" );
  77.     if ( pm_writebiglong( stdout, rows ) == -1 )
  78.     pm_error( "write error" );
  79.     if ( pm_writebigshort( stdout, (short) 1 ) == -1 )
  80.     pm_error( "write error" );
  81.  
  82.     item = 0;
  83.     bitsperitem = 0;
  84.     bitshift = 7;
  85.     }
  86.  
  87. #if __STDC__
  88. static void
  89. putbit( bit b )
  90. #else /*__STDC__*/
  91. static void
  92. putbit( b )
  93.     bit b;
  94. #endif /*__STDC__*/
  95.     {
  96.     if ( bitsperitem == 8 )
  97.     putitem( );
  98.     if ( b == PBM_WHITE )
  99.     item += 1 << bitshift;
  100.     bitsperitem++;
  101.     bitshift--;
  102.     }
  103.  
  104. static void
  105. putrest( )
  106.     {
  107.     if ( bitsperitem > 0 )
  108.     putitem( );
  109.     }
  110.  
  111. static void
  112. putitem( )
  113.     {
  114.     (void) putc( item, stdout );
  115.     item = 0;
  116.     bitsperitem = 0;
  117.     bitshift = 7;
  118.     }
  119.